// Playground - noun: a place where people can play

import UIKit
import SwiftyJSON
import Alamofire
import Foundation
import ObjectMapper
class HomeModel: Mappable{
    
    var id:Int!
    
    var gmtCreate:Double!
    
    var gmtModified:Double!
    
    var type:Int!
    
    var contentType:Int!
    
    var contentId:String!
    
    var title:String!
    
    var picUrl:String!
    
    var top:String!
    
    var subtitle:String!
    
    var description:String!
    
    required init?(_ map: Map){
        
    }
    func mapping(map: Map){
        
        id <- map["id"]
        gmtCreate <- map["gmtCreate"]
        gmtModified <- map["gmtModified"]
        type <- map["type"]
        contentType <- map["contentType"]
        contentId <- map["contentId"]
        title <- map["title"]
        picUrl <- map["picUrl"]
        top <- map["top"]
        subtitle <- map["subtitle"]
        description<-map["description"]
        
        
    }
}


func isError(error: ErrorType?, failure: (code: Int, message: String) -> Void) -> Bool {
    if (error != nil) {
        failure(code: -1, message: "网络发生异常")
        return true
    }
    return false
}


func getHomeData(successed:(data:[HomeModel])->Void,failure:(code:Int,message:String)->Void){
    
    Alamofire.request(.GET, "https://www.baidu.com").responseJSON {
        (request,response,result) -> Void in
        
        print(request?.HTTPBody?.length)
        if isError(result.error, failure: failure){
            return
        }
        print(result.value)
        let homedatas=Mapper<HomeModel>().mapArray(result.value)
        successed(data: homedatas!)
    }
    
}

let success={
    
    (data:[HomeModel]) -> Void in
   print(data.count)
};

let failure = {
    (code:Int,message:String) -> Void in
  print("code:\(code),message:\(message)")
}
getHomeData(success,failure: failure)






